package gov.pbc.nn.kjgl.gzjh.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import gov.pbc.nn.core.dao.AbstractJdbcDao;
import gov.pbc.nn.core.utils.EntityRowMapperUtils;
import gov.pbc.nn.kjgl.gzjh.entity.WorkP;
import gov.pbc.nn.kjgl.gzjh.entity.WorkPlanBanch;

@Repository
public class WorkJdbcDao extends AbstractJdbcDao {

	// * 0是初始状态，1是kz操作中;2处理人操作；3已完成
	public void getAllkz(Page<WorkP> page, Criteria criteria) {
		// String sql =
		// String sql =
		// "select  w.*  from t_work_plan w INNER JOIN t_work_plan_banch B ON B.WORK_ID_=w.ID_ where  B.DEPT_ID_=:deptId and   B.INVALID_ =0 and w.INVALID_=0 and B.BANCH_USERNAME_=:banchUsername  and w.STATE_ in (1,2,3)";
		String sql = "select  w.*  from t_work_plan w INNER JOIN t_work_plan_banch B ON B.WORK_ID_=w.ID_ where  B.DEPT_ID_=:deptId and   B.INVALID_ =0 and w.INVALID_=0 and B.BANCH_USERNAME_=:banchUsername ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId());
		map.put("banchUsername", ContextHolder.getLoginUserName());
		ParseResult pr = parseCriteria(criteria, true, null);
		if (pr != null) {
			StringBuffer sbSql = pr.getAssemblySql();
			sql += " and " + sbSql.toString() + " order by w.CREATE_DATE_ desc ";
			map.putAll(pr.getValueMap());
		} else {
			sql += " order by w.CREATE_DATE_ desc ";
		}
		// this.pagingQuery(page, sql, new RowMapper<WorkP>() {
		// public WorkP mapRow(ResultSet rs, int rowNum) throws SQLException {
		// // TODO Auto-generated method stub
		// return workJdbcDao.this.mapRow(rs, rowNum);
		// }
		// }, map);
		this.pagingQuery(page, sql, new RowMapper<WorkP>() {

			@Override
			public WorkP mapRow(ResultSet rs, int rowNum) throws SQLException {
				return EntityRowMapperUtils.get(WorkP.class, rs, rowNum);
			}
		}, map);
	}

	public WorkP mapRow(ResultSet rs, int rowNum) throws SQLException {
		WorkP p = new WorkP();
		p.setId(rs.getString("ID_"));
		p.setCreateDate(rs.getDate("CREATE_DATE_"));
		p.setUpdateDate(rs.getDate("UPDATE_DATE_"));
		p.setCreateUser(rs.getString("CREATE_USER_"));
		p.setUpdateUser(rs.getString("UPDATE_USER_"));
		p.setEndDate(rs.getDate("END_DATE_"));
		p.setInvalid(rs.getBoolean("INVALID_"));
		p.setPlayDecompose(rs.getString("PLAY_DECOMPOSE_"));
		p.setSpecificRequirement(rs.getString("SPECIFIC_REQUIREMENT_"));
		p.setStartDate(rs.getDate("START_DATE_"));
		p.setState(rs.getInt("STATE_"));
		p.setWorkName(rs.getString("WORK_NAME_"));

		try {
			p = EntityUtils.toEntity(p);
			EntityUtils.setValue(p, "banchUsername", rs.getString("BANCH_USERNAME_"));// 半场Username虚拟一个属性用来查找
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 处理人页面实际加载方法，根据部门和处理人username查找符合登陆人要求的WrokPlan
	 *
	 *@auther jack.zhao 
	 *@param page
	 *@param criteria
	 *
	 */
	public void getAllUser(Page<WorkP> page, Criteria criteria) {

		// String sql =
		// "select  w.*  from t_work_plan w INNER JOIN t_work_plan_banch PB ON PB.WORK_ID_=w.ID_ INNER JOIN t_work_plan_process_user PU ON  PU.BANCH_ID_=PB.ID_ where  PB.DEPT_ID_=:deptId and PU.INVALID_=0 and  w.INVALID_=0 and  PU.PROCESS_USERNAME_=:userName and w.STATE_ in (2,3) ";
		String sql = "select  w.*,PB.BANCH_USERNAME_  from t_work_plan w INNER JOIN t_work_plan_banch PB ON PB.WORK_ID_=w.ID_ INNER JOIN t_work_plan_process_user PU ON  PU.BANCH_ID_=PB.ID_ where  PB.DEPT_ID_=:deptId and PU.INVALID_=0 and  w.INVALID_=0 and  PU.PROCESS_USERNAME_=:userName ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", ContextHolder.getLoginUser().getDepts().get(0).getId());
		map.put("userName", ContextHolder.getLoginUserName());
		ParseResult pr = parseCriteria(criteria, true, null);
		if (pr != null) {
			StringBuffer sbSql = pr.getAssemblySql();
			sql += " and " + sbSql.toString() + " order by w.CREATE_DATE_ desc ";
			map.putAll(pr.getValueMap());
		} else {
			sql += " order by w.CREATE_DATE_ desc ";
		}
		// this.
		this.pagingQuery(page, sql, new RowMapper<WorkP>() {
			public WorkP mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				return WorkJdbcDao.this.mapRow(rs, rowNum);
			}
		}, map);
	}

//	public List<WorkPlanBanch> getWorkBanchByDeptProcess(String id) {
//		String sql = "SELECT PB.* FROM t_work_plan_banch PB INNER JOIN t_work_plan_process_user PU ON PU.BANCH_ID_ = PB.ID_ WHERE PU.INVALID_ = 0 AND PB.INVALID_ = 0 AND PB.DEPT_ID_ =? AND PU.PROCESS_USERNAME_ =? AND PB.WORK_ID_ =? order by PB.CREATE_DATE_ desc ";
//		// Map<String, Object> map2 = new HashMap<String, Object>();
//		// map2.put("userName", ContextHolder.getLoginUserName());
//		// map2.put("deptId",
//		// ContextHolder.getLoginUser().getDepts().get(0).getId());
//		// map2.put("workId", id);
//		String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();
//		String userName = ContextHolder.getLoginUserName();
//		List<WorkPlanBanch> query = this.getJdbcTemplate().query(sql, new Object[] { deptId, userName, id }, new RowMapper<WorkPlanBanch>() {
//			@Override
//			public WorkPlanBanch mapRow(ResultSet rs, int rowNum) throws SQLException {
//				// TODO Auto-generated method stub
//				return EntityRowMapperUtils.get(WorkPlanBanch.class, rs, rowNum);
//			}
//		});
//		return query;
//	}
	
	/**
	 * 根据workId和banchUsername返回分班人实体
	 *
	 *@auther jack.zhao 
	 *@param id
	 *@param banchUsername
	 *@return
	 *
	 */
	public List<WorkPlanBanch> getWorkBanchByDeptProcess(String id,String banchUsername) {
		String sql = "SELECT PB.* FROM t_work_plan_banch PB INNER JOIN t_work_plan_process_user PU ON PU.BANCH_ID_ = PB.ID_ WHERE PU.INVALID_ = 0 AND PB.INVALID_ = 0 AND PB.DEPT_ID_ =? AND PU.PROCESS_USERNAME_ =? AND PB.WORK_ID_ =?  and PB.BANCH_USERNAME_ = ? order by PB.CREATE_DATE_ desc ";
		String deptId = ContextHolder.getLoginUser().getDepts().get(0).getId();
		String userName = ContextHolder.getLoginUserName();
		List<WorkPlanBanch> query = this.getJdbcTemplate().query(sql, new Object[] { deptId, userName, id ,banchUsername}, new RowMapper<WorkPlanBanch>() {
			@Override
			public WorkPlanBanch mapRow(ResultSet rs, int rowNum) throws SQLException {
				return EntityRowMapperUtils.get(WorkPlanBanch.class, rs, rowNum);
			}
		});
		return query;
	}
}
